package com.RequestYunZhuWang;

/**
 * 云筑网请求接口入口
 */
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.yishun.springboot.dao.CWRServiceTest;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Hello world!
 *
 */

@Slf4j
public class AppChange 
{
    public static void main( String[] args ) throws UnsupportedEncodingException
    {
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
        System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout");

        String url="http://lwapi.yzw.cn/open.api";
        String appid="ysznkj";
        String projectcode="321837";
        
        String appkey=   "4743f46c6c9e49c3b9de8064372a3447";      //中建三局统一是该key,该key是申请提交后云筑网发送到接收邮箱的秘钥
        String projectkey="86c6ef8ef5514a0eae7b1556765f87b3";     //根据登录云筑网账号查看项目编号（projectcode）对应的数据验证密钥
        
        String appsecret=appkey+projectkey;
        String method="Common.GetAreaList";
        String version="1.0";
        
        Map rawData = new HashMap();
        rawData.put("TimeStamp", "2017-01-01 12:01:01.001");
        rawData.put("SK", "");
        
        String data = JSON.toJSONString(rawData);
        


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        String sortString = "appid=" + appid;
        String guid = UUID.randomUUID().toString().replace("-","");
        String timestamp = sdf.format(new Date());

        sortString += "&data=" + data;
        sortString += "&format=json";
        sortString += "&method=" + method;
        sortString += "&nonce=" + guid;
        sortString += "&timestamp=" + timestamp;
        sortString += "&version=" + version;
        sortString += "&Appsecret=" + appsecret;

        String sign = EncriptionHelper.getSignatureWithMD5(sortString.toLowerCase());
        log.info(String.format("加密后签名串为：%s",sign));

        BaseRequest request = new BaseRequest(){};
        Map<String,String> dataMap = new HashMap<>();
        dataMap.put("appid",appid);
        dataMap.put("projectcode",projectcode);
        dataMap.put("data", data);


        dataMap.put("format","json");
        dataMap.put("method",method);
        dataMap.put("nonce",guid);
        dataMap.put("timestamp",timestamp);
        dataMap.put("version",version);
        dataMap.put("sign",sign);
        try {
            String res = request.postData(url,null,dataMap);
//            dataMap.put("appsecret",appsecret);
            WriteLogInfo(dataMap);
            log.info(String.format("调用结果：%s",res));
        } catch (Exception ex) {
            String errorMsg = "";
            if (ex.getMessage().contains("ClientProtocolException")) {
                errorMsg = "异常：输入的URL无效";
            } else if (ex.getMessage().contains("UnknownHostException") || ex.getMessage().contains("unreachable")) {
                errorMsg = "异常：输入的URL不正确";
            }
            log.info(errorMsg);
        }
    }


    private static void WriteLogInfo(Map<String,String> map)
    {
        String paraString = "";
        StringBuilder sb = new StringBuilder();
        sb.append("appid=" + map.get("appid") + "&");
        sb.append("data=" + map.get("data") + "&");
        sb.append("format=" + map.get("format") + "&");
        sb.append("method=" + map.get("method") + "&");
        sb.append("nonce=" + map.get("nonce") + "&");
        sb.append("timestamp=" + map.get("timestamp") + "&");
        sb.append("version=" + map.get("version") + "&");
        sb.append("appsecret=" + map.get("appsecret") + "&");
        sb.append("sign=" + map.get("sign"));
        paraString = sb.toString();
        paraString = paraString.replace("\r", "").replace("\n", "").replace(" ","");
        log.info(String.format("请求FormData:%s \r\n",paraString));
    }

    private static String acceptmultiLineChars2(Scanner sc)
    {
        ArrayList<String> ns = new ArrayList<>();
        do {
            String str = sc.nextLine();
            if (str.equals("")) {
                break;
            }
            ns.add(str);
        } while (true);
        StringBuilder sb = new StringBuilder();
        if (ns.size() > 0) {
            ns.forEach(item -> sb.append(item + "\r\n"));
        }
        return sb.toString();
    }
}
